package com.powernobug.service.impl;

import com.powernobug.bean.model.MarketGoodsAttribute;
import com.powernobug.bean.model.MarketGoodsProduct;
import com.powernobug.bean.model.MarketGoodsSpecification;
import com.powernobug.bean.vo.param.GoodsCreateParam;
import com.powernobug.mapper.*;
import com.powernobug.service.AdminGoodsService;
import com.powernobug.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;

/**
 * @Author: 肖云戈
 * @CreateTime: 2024-08-22
 * @Description:
 */
public class AdminGoodsServiceImpl implements AdminGoodsService {
    private static SqlSession tempSession;

    @Override
    public boolean creatGood(GoodsCreateParam goodsCreateParam) {
        // 先校验一下 brand（品牌商） 和 category（商品分类）是否在数据库中有记录
        tempSession = MyBatisUtil.getSqlSession();
        MarketCategoryMapper categoryMapper = tempSession.getMapper(MarketCategoryMapper.class);
        MarketBrandMapper brandMapper = tempSession.getMapper(MarketBrandMapper.class);
        boolean isCategoryExist = categoryMapper.selectByPrimaryKey(goodsCreateParam.getGoods().getCategoryId()) != null;
        boolean isBrandExist = brandMapper.selectByPrimaryKey(goodsCreateParam.getGoods().getBrandId()) != null;
        if (!isCategoryExist || !isBrandExist)
            return false;

        // 校验通过后，直接向数据库中新增记录即可
        // 先增加 market_goods 表中的数据，并获取插入后的 id （useGenericKey=true）
        MarketGoodsMapper goodsMapper = tempSession.getMapper(MarketGoodsMapper.class);
        // 插入前记得更新对象中的时间类型字段，及deleted=0
        goodsCreateParam.getGoods().setAddTime(new Date());
        goodsCreateParam.getGoods().setUpdateTime(new Date());
        goodsCreateParam.getGoods().setDeleted(false);
        goodsMapper.insert(goodsCreateParam.getGoods());

        Integer goodsId = goodsCreateParam.getGoods().getId();

        // 获取到 goodsId 后，继续进行后续插入操作
        // market_goods_product 表的插入
        MarketGoodsProductMapper marketGoodsProductMapper = tempSession.getMapper(MarketGoodsProductMapper.class);
        for (MarketGoodsProduct goodsProduct : goodsCreateParam.getProducts()) {
            goodsProduct.setGoodsId(goodsId);
            goodsProduct.setAddTime(new Date());
            goodsProduct.setUpdateTime(new Date());
            goodsProduct.setDeleted(false);
            marketGoodsProductMapper.insert(goodsProduct);
        }

        // market_goods_attribute 表的插入
        MarketGoodsAttributeMapper goodsAttributeMapper = tempSession.getMapper(MarketGoodsAttributeMapper.class);
        for (MarketGoodsAttribute goodsAttribute : goodsCreateParam.getAttributes()) {
            goodsAttribute.setGoodsId(goodsId);
            goodsAttribute.setAddTime(new Date());
            goodsAttribute.setUpdateTime(new Date());
            goodsAttribute.setDeleted(false);
            goodsAttributeMapper.insert(goodsAttribute);
        }

        // market_goods_specification 表的插入
        MarketGoodsSpecificationMapper specificationMapper = tempSession.getMapper(MarketGoodsSpecificationMapper.class);
        for (MarketGoodsSpecification goodsSpecification : goodsCreateParam.getSpecifications()) {
            goodsSpecification.setGoodsId(goodsId);
            goodsSpecification.setAddTime(new Date());
            goodsSpecification.setUpdateTime(new Date());
            goodsSpecification.setDeleted(false);
            specificationMapper.insert(goodsSpecification);
        }

        // 全部插入完成，提交事务，返回 true
        MyBatisUtil.releaseSqlSession(tempSession);
        return true;
    }
}
